<div id="avatar-title">
		<h3>设置头像</h3>
		<p class="desc">请上传您最满意的照片</p>
</div>


<div id="avatar-contents" class="clearfix">
<?php
	$uid=$this->session->userdata('user_id');
	$defualt=base_url().'avatars/default.jpg';
	$file_path= substr($photo_path,strlen(base_url()));
		?>
		
		<div class="avatar-content">
		<form id="avatar-form" class="avatar-form" method="post" action="/settings/avatar-update" enctype="multipart/form-data">
		
		<?php
		if($photo_path !="" && $file_path !="avatars/default.jpg" && file_exists($file_path)){
				?>
					<img src="<?php echo $photo_path; ?>" alt="" class="avatar-img">
					<div class="avatar-actions clearfix">
							<span class="upload-avatar">
								<span class="btn-avatar">重新上传</span>
								<input id="avatar" type="file" onchange="avatarUpload();" size="1" class="avatar-file" name="avatar">
							</span>
							<span class="delete-avatar"><a href="javascript:avatarDelte()">还原默认</a></span>
					</div>
				</form>
			</div>		
				<?php			
			
		}
		else
		{
			?>
				<div class="empty-avatar">
						<img src="<?php echo base_url() ?>avatars/default.jpg" title="上传头像" alt="" class="avatar-img">
						<input id="avatar" type="file" onchange="avatarUpload();" class="avatar-file" size="1" name="avatar">
						<p class="hint">点击图片上传</p>
					</div>
					<div style="display:none" class="avatar-actions clearfix">
						<span class="upload-avatar">
							<span class="btn-avatar">重新上传</span>
							<span class="btn-file"></span>
						</span>
					<span class="delete-avatar"><a href="javascript:avatarDelte()">还原默认</a></span>
					</div>
			</form>
		</div>			
		<?php
		}
		?>
</div>


<a class="btn-back" href="<?php echo base_url()?>setting">返回</a>
<img class="loading-pic" style="display:none;" src="<?php echo base_url() ?>images/loading.gif">
<img class="default-pic" style="display:none;" src="<?php echo base_url() ?>avatars/default.jpg"/>

<script type='text/javascript' src="<?php echo base_url();?>/js/ajaxfileupload.js"></script>

<script type="text/javascript">
	
	function avatarUpload()
	{
		$(".empty-avatar").ajaxStart(function(){
			$(".avatar-actions").show();
			var imgsrc=$(".default-pic").attr("src");
			$(".empty-avatar").after("<img src='"+imgsrc+"'  alt='' class='avatar-img'/>");
			$(".empty-avatar").detach();
			$(".btn-file").after("<input id='avatar' type='file'  onchange='ajaxFileUpload();' class='avatar-file' size='1' name='avatar'>");
			$(".btn-file").detach();
		});
		
		$(".btn-avatar")
		.ajaxStart(function(){
			$(this).html("<img class='loading' src='"+$(".loading-pic").attr("src")+"'/>");
		})
		.ajaxComplete(function(){
			$(this).html("重新上传");
		});
		
		$.ajaxFileUpload
		(
			{
				url:'avatarupload',
				secureuri:false,
				fileElementId:'avatar',
				dataType: 'json',
				data:{name:'avatar', path:'avatars'}, //data是传给后台的值
				success: function (data, status)
				{
					if(typeof(data.error) != 'undefined')
					{
						if(data.error != '')
						{
							alert(data.error);
						}else
						{
							$(".avatar-img").attr("src",data.msg);
						}
					}
				},
				error: function (data, status, e)
				{
					alert(e);
				}
			}
		)
		
		return false;
	}
		
	function avatarDelte(){
		if(window.confirm("真的要删除这个头像吗？"))
		{
			var avatarPath=$(".avatar-img").attr("src");
			var imgsrc=$(".default-pic").attr("src");
			
			
			$.ajax({
	  		type: 'POST',
	  		url: "avatardelete",
	  		data: {path:avatarPath},
	  		dataType: 'json',
	  		success: function(data,status)
	  		{
	  			if(typeof(data.error) != 'undefined')
					{
						if(data.error != '')
						{
							alert(data.error);
						}else
						{
							$(".avatar-img").attr("src",imgsrc);
						}
					}
	  		},
	  		error: function (data, status, e)
				{
					alert(e);
				}	  		
			});
		}
	}
</script>